using System;
using System.IO;
using System.Windows.Forms;
using ConfigurationSettings;
using ServiceLocator;

namespace FileTracker.Forms
{
    public partial class FormAddOpenWith : Form
    {
        public FormAddOpenWith()
        {
            InitializeComponent();
        }

        private void buttonOK_Click(object sender, EventArgs argEventArgs)
        {
            //ApplicationSettings ConfigFile;
            string OpenWithPath = textBoxFile.Text;
            string OpenWithName = textBoxName.Text;

            //Check for path exists
            if (!File.Exists(OpenWithPath))
            {
                MessageBox.Show("That file does not exist.  Please try again.", "Invalid \"Open With\" path");
                textBoxFile.Focus();
                return;
            }

            //Load the config file
            //ConfigFile = new ApplicationSettings();
	        IApplicationSettings localSettings = Locator.Resolve<IApplicationSettings>();

            //Check the current favorites for duplicate names.
			if (localSettings.FavoriteList.ContainsKey(OpenWithName))
            {
                MessageBox.Show("There is already an \"Open With\" with that name.  Please try again.", "Invalid \"Open With\" name");
                return;
            }

            //Check the current favorites for duplicate locations
			if (localSettings.FavoriteList.ContainsValue(OpenWithPath))
            {
                MessageBox.Show("There is already an \"Open With\" with that path.  Please try again.", "Invalid \"Open With\" path");
                return;
            }

			localSettings.ProgramList.Add(OpenWithName, OpenWithPath);

			localSettings.Save();
            //ApplicationSettings = null;

            //Close this window.
            this.Close();
        }

        private void buttonFileBrowse_Click(object sender, EventArgs argEventArgs)
        {
            OpenFileDialog BrowseDefault;

            //Open directory browser
            BrowseDefault = new OpenFileDialog();
            BrowseDefault.AutoUpgradeEnabled = true;
            BrowseDefault.CheckFileExists = true;
            BrowseDefault.RestoreDirectory = false;
            BrowseDefault.Title = "Select Program";
            BrowseDefault.InitialDirectory = Environment.SpecialFolder.DesktopDirectory.ToString();

            BrowseDefault.ShowDialog(this);
            textBoxFile.Text = BrowseDefault.FileName;

        }

    }
}